﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Diagnostics;

using ExtensibleEventViewer.Core.Interfaces;
using ExtensibleEventViewer.Core.Primitives;


namespace ExtensibleEventViewer.Providers
{
	/// <summary>
	/// Defines methods for retrieving event log records
	/// </summary>
	[Export(typeof(IEventLogProvider))]
	[ExportMetadata("ProviderID", PROVIDER_ID)]
	public class EventLogProvider : IEventLogProvider
	{
		private const string PROVIDER_ID = "2d7859fe-0278-4e0b-a110-d44cf83d235f";

		public IEnumerable<EventLogRecord> Select(NameValueCollection selectionCriteria, string computerName)
		{
			return null;
		}

		/// <summary>
		/// Selects list of available Event Logs on specific computer
		/// </summary>
		/// <returns>List of available Event Logs</returns>
		public IEnumerable<EventLogEntity> GetEventLogList(string computerName)
		{
			if (string.IsNullOrEmpty(computerName))
			{
				return EventLog.GetEventLogs().ConvertToEventLogEntityArray();
			}

			return EventLog.GetEventLogs(computerName).ConvertToEventLogEntityArray();
		}

		public EventLogEntity GetEventLog(string name, string computerName)
		{
			if (!EventLog.Exists(name, computerName))
			{
				return null;
			}

			EventLog e = new EventLog(name, computerName);

			return e.ConvertToEventLogEntity();
		}

		public string ProviderName
		{
			get
			{
				return "Windows EventLog Provider";
			}
		}

		public string ProviderID
		{
			get
			{
				return PROVIDER_ID;
			}
		}

		public override string ToString()
		{
			return ProviderName;
		}
	}
}
